package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.SysSyConfig;
import cn.yuanqiao.archive.appManager.service.ISysSyConfigService;
import cn.yuanqiao.archive.appManager.service.ITenantSysDictTypeService;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.domain.entity.SysRole;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.service.ISysRoleService;
import cn.yuanqiao.system.service.ISysUserService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zjj
 * @date 2023/8/4
 */
@RestController
@RequestMapping("/archive/syConfig")
public class SysSyConfigController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISysSyConfigService sysSyConfigService;
    @Autowired
    private ISysUserService remoteUserService;

    @Autowired
    private ISysRoleService sysRoleService;
    @Value("${YQDA_SYSTEM_NAME}")
    private String YQDA_SYSTEM_NAME;
    @Value("${YQDA_SYSTEM_COPYRIGHT}")
    private String YQDA_SYSTEM_COPYRIGHT;
    @Value("${YQDA_SYSTRM_LOG}")
    private String YQDA_SYSTRM_LOG;
    @Value("${YQDA_MJ_LEVEL}")
    private String YQDA_MJ_LEVEL;
    @Autowired
    private ITenantSysDictTypeService dictTypeService;

    @Log(title = "获取首页配置列表", businessType = BusinessType.SELECT)
    @PostMapping("/getSyConfig")
    public AjaxResult getSyConfig(){
        //SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleKey();
        //dagly = 档案管理员的； normal = 普通用户 ； zsdagly = 展示内容  kjdagly = 快捷入口  ； type =  1 = 展示内容 ； type =  2 快捷入口  3 各项待办
        JSONArray object = new JSONArray();
        List<SysRole> roleList = sysRoleService.selectRoleList(new SysRole(SecurityUtils.getTenantCode()));
        for(SysRole role:roleList){
            List<SysSyConfig> zsdagly = sysSyConfigService.selectSysSyConfig("'"+role.getRoleKey()+"'",1);
            List<SysSyConfig> kjdagly = sysSyConfigService.selectSysSyConfig("'"+role.getRoleKey()+"'",2);
            List<SysSyConfig> dbdagly = sysSyConfigService.selectSysSyConfig("'"+role.getRoleKey()+"'",3);
            List<SysSyConfig> grzx = sysSyConfigService.selectSysSyConfig("'"+role.getRoleKey()+"'",4);

            if(zsdagly.size()>0 || kjdagly.size()>0 || dbdagly.size()>0){
                JSONObject data1 = new JSONObject();
                data1.put("name",role.getRoleName());
                data1.put("key",role.getRoleKey());
                data1.put("zs",zsdagly);
                data1.put("kj",kjdagly);
                data1.put("db",dbdagly);
                data1.put("grzx",grzx);
                object.add(data1);
            }
        }
            /*List<SysSyConfig> zsdagly = sysSyConfigService.selectSysSyConfig("dagly",1);
            List<SysSyConfig> kjdagly = sysSyConfigService.selectSysSyConfig("dagly",2);
            List<SysSyConfig> dbdagly = sysSyConfigService.selectSysSyConfig("dagly",3);
            JSONObject dagly = new JSONObject();
            dagly.put("zsdagly",zsdagly);
            dagly.put("kjdagly",kjdagly);
            dagly.put("dbdagly",dbdagly);

            List<SysSyConfig> zsnormal = sysSyConfigService.selectSysSyConfig("normal",1);
            List<SysSyConfig> kjnormal = sysSyConfigService.selectSysSyConfig("normal",2);
            List<SysSyConfig> dbnormal = sysSyConfigService.selectSysSyConfig("normal",3);
            JSONObject normal = new JSONObject();
            normal.put("zsnormal",zsnormal);
            normal.put("kjnormal",kjnormal);
            normal.put("dbnormal",dbnormal);

            JSONObject object = new JSONObject();
            object.put("dagly",dagly);
            object.put("normal",normal);*/


        return AjaxResult.success(object);
    }

    @Log(title = "获取首页配置字典", businessType = BusinessType.SELECT)
    @PostMapping("/getSyConfigName")
    public AjaxResult getSyConfigName(){
        return AjaxResult.success(sysSyConfigService.getSyConfigName());
    }

    @Log(title = "保存首页配置", businessType = BusinessType.INSERT)
    @PostMapping("/saveSyConfig")
    public AjaxResult saveSyConfig(@RequestParam("roleName")String roleName,@RequestParam("nameList")String nameList,@RequestParam("type")Integer type){
        sysSyConfigService.saveSysSyConfig(roleName,nameList,type);
        return AjaxResult.success();
    }

    @GetMapping("/systemName")
    public AjaxResult systemName(){
        JSONObject object = new JSONObject();
        object.put("YQDA_SYSTEM_NAME",YQDA_SYSTEM_NAME);
        object.put("YQDA_SYSTEM_COPYRIGHT",YQDA_SYSTEM_COPYRIGHT);
        object.put("YQDA_MJ_LEVEL",YQDA_MJ_LEVEL);
        object.put("YQDA_SYSTRM_LOG",YQDA_SYSTRM_LOG);
        logger.info(object.toJSONString());
        return AjaxResult.success(object);
    }

    @GetMapping("/ofdReadName")
    public AjaxResult ofdReadName(){
        return AjaxResult.success(dictTypeService.selectDictDataByType("OfdReadName"));
    }

}
